<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/06/25
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\common\controller\Api;
use think\Config;
use think\Db;

use Yurun\Util\Chinese\Pinyin;
use Yurun\Util\Chinese;

// use app\common\exception\UploadException;
// use app\common\library\Upload;

/**
 * 公共无需登陆接口
 */
class Com extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

  
    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test()
    {
        $this->success('返回成功', ['com' => 'TpMeCMS']);
    }

    /**
     * 获取banner
     *
     * @return void
     */
    public function get_banner()
    {
        $this->success('ok',Db::name('banner')->order('weigh desc')->select());
    }

    /**
     * 获取配置
     *
     * @param string $field 获取需要的字段例如name（可不传获取全部
     * @return void
     */
    public function get_config($field = null)
    {
        if($field){
            $this->success($field,Config::get('site.'.$field));
        }else{
            $this->success('all',Config::get('site'));
        }
    }

    /**
     * 获取需要添加的字体
     *
     * @param string $file_path 上传的图片路径
     * @param string $p_id 第几盘id
     * @param string $b_id 第几版id
     * @return void
     */
    public function get_need_add_font($file_path='/uploads/20210826/0059fc4254db56c3008825a1113cb349.jpeg',$p_id=7,$b_id=5)
    {
        // http://font.he4966.cn/api/com/get_need_add_font?b_id=5&p_id=1&file_path=/uploads/20210828/d3c356f251415bddd15ad0de68e20ec4.jpeg
        
        // $file_path = ''
        // $url = 'http://49.234.127.77:8001/predict';
        $url = Config::get('site.post_url');
        $path = $file_path;
      
        if(!strstr('http',$path)){
            $path = $this->request->domain().$path; 
        }

        // halt($path);

        $str = "curl -X POST -F image_path=".$path." '".$url."'";
        $res = shell_exec($str);
        $json = json_decode($res,true);

        $font_arr = [];
        // halt($json);
        if($json['success']){
            $arr = $json['results'];
            $pai = 1;
            $Y = 0;
            $data['p_id'] = $p_id;
            $data['b_id'] = $b_id;
            foreach ($arr as $H => $val) {
                if(($H%4)==0){
                    $pai = 1;
                    $Y++;
                }
                $data['y'] = $Y;
                // halt($val);
                foreach ($val as $x => $v) {
                    // halt($v);
                    $data['x'] = $x+1;
                    if($v<1){    //需要补字
                        // halt($data);
                        $font = Db::name('font')->where($data)->find();
                        // halt($font);
                        if(isset($font_arr[$font['id']])){
                            $font_arr[$font['id']]['num']  = $font_arr[$font['id']]['num'] +1;
                        }else{
                            $font_arr[$font['id']] = $font;
                            $font_arr[$font['id']]['num']  = 1;
                           
                        }
                    }
                }

                
            }
            $list = [];
            if($font_arr){
                foreach ($font_arr as $key => $val) {
                    $list[] = $val;
                }
            }

            

            $this->success('ok',$list);
        }else{
            $this->error('解析失败',$json);
        }
        


    }


    /**
     * 获取字体分类
     *
     * @return void
     */
    public function get_fontcate_all_list()
    {
        $list = Db::name('fontcate')->select();
        $this->success('ok',$list);
    }

    /**
     * 获取版全部列表
     *
     * @return void
     */
    public function get_b_all_list()
    {
        $list = Db::name('b')->select();
        $this->success('ok',$list);
    }


    /**
     * 获取版列表
     * @param int $fontcate_id 分类id
     * @return void
     */
    public function get_b_list($fontcate_id)
    {
        $list = Db::name('b')->where('fontcate_id',$fontcate_id)->select();
        $this->success('ok',$list);
    }

    /**
     * 获取盘列表
     * @param int $b_id 版id
     * @return void
     */
    public function get_p_list($b_id)
    {
        $list = Db::name('p')->where('b_id',$b_id)->select();
        $this->success('ok',$list);
    }

    /**
     * 获取字的id
     *
     * @param int $b_id 版id
     * @param int $p_id 盘id
     * @param string $font 字
     * @return void
     */
    public function get_font_id($b_id,$p_id,$font)
    {
        $data['b_id'] = $b_id;
        $data['p_id'] = $p_id;
        $data['font'] = $font;
        $res = Db::name('font')->where($data)->find();
        if($res){
            $this->success('ok',$res);
        }else{
            $this->error('未找到');
        }
    }

    /**
     * 获取某个盘里面的字体列表
     *
     * @param Type|null $var
     * @return void
     */
    public function get_p_font_list($p_id)
    {
        $list = Db::name('font')->where('p_id',$p_id)->select();
        $this->success(sizeof($list),$list);
    }



    /**
     * 汉字转拼音首字母
     *
     * @param string $font 汉字
     * @return void
     */
    public function font_toPinyin($font=null)
    {
        if($font){
            $initial = Chinese::toPinyin($font, Pinyin::CONVERT_MODE_PINYIN_FIRST);
            $this->success('ok',$initial);
        }else{
            $list = Db::name('font')->where('initial',null)->select();
            // dump($res);
            $arr = [];
            $ci=0;
            foreach ($list as $key => $res) {
                $font = $res['font'];
                $initial = Chinese::toPinyin($font, Pinyin::CONVERT_MODE_PINYIN_FIRST);
                unset($temp);
                // $temp['font'] = $font;
                // $Font = new \app\admin\model\Font();
                // $row = $Font->get($res['id']);

                $temp['initial'] = $initial['pinyinFirst'][0][0];
                if(sizeof($initial['pinyinFirst'])>1){
                    $temp['initial2'] = $initial['pinyinFirst'][1][0];
                }

                $up_res = Db::name('font')->where('id',$res['id'])->update($temp);
                // Db::commit();
                if($up_res){
                    $ci++;
                }
               
                $arr[] = $temp;
            }
            $this->success($ci,$arr);

        }
    }

}
